#include "commanpage.h"
#include "ui_commanpage.h"

#include "listitembox.h"
#include <QDebug>

CommanPage::CommanPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CommanPage)
{
    ui->setupUi(this);

    //删除水平滚动条
    ui->pageMusicList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    //播放全部
    connect(ui->playAllBtn , &QPushButton::clicked , this , [=](){
        emit playAll(_pageType);
    });

    //双击ItemBox
    connect(ui->pageMusicList , &QListWidget::doubleClicked , this , [=](const QModelIndex &index){
        emit playMusicByIndex(this , index.row());
    });
}

CommanPage::~CommanPage()
{
    delete ui;
}

void CommanPage::setCommanPageUi(const QString &text, const QString &imagePath)
{
    //设置文字
    ui->pageTitle->setText(text);
    //设置图片
    ui->musicImageLabel->setPixmap(QPixmap(imagePath));
    //设置图片的缩放
    ui->musicImageLabel->setScaledContents(true);
}

void CommanPage::setPageType(PageType pageType)
{
    _pageType = pageType;
}

void CommanPage::setMusicImage(QPixmap pixmap)
{
    ui->musicImageLabel->setPixmap(pixmap);
    ui->musicImageLabel->setScaledContents(true);
}

void CommanPage::addMusicToMusicPage(MusicList &musicList)
{
    musicOfPage.clear();

    for(auto& music : musicList)
    {
        switch(_pageType)
        {
        case PAGE_LOCAL:
                musicOfPage.push_back(music.getMusicID());
            break;
        case PAGE_LIKE:
            if(music.getIsLike() == true)
                musicOfPage.push_back(music.getMusicID());
            break;
        case PAGE_HISTORY:
            if(music.getIsHistory() == true)
                musicOfPage.push_back(music.getMusicID());
            break;
        default:
            break;
        }
    }
}

void CommanPage::reFresh(MusicList &musicList)
{
    ui->pageMusicList->clear();

    addMusicToMusicPage(musicList);

    for(auto& musicid : musicOfPage)
    {
        auto it = musicList.findMusicById(musicid);
        if(it == musicList.end())
            continue;
        //歌曲在当前页面显示
        ListItemBox* itemBox = new ListItemBox(ui->pageMusicList);
        itemBox->setMusicName(it->getMusicName());
        itemBox->setMusicSigner(it->getSignerName());
        itemBox->setMusicAlbum(it->getAlbumName());
        itemBox->setLikeMusic(it->getIsLike());

        QListWidgetItem* item = new QListWidgetItem(ui->pageMusicList);
        item->setSizeHint(QSize(ui->pageMusicList->width() , 45));
        ui->pageMusicList->setItemWidget(item , itemBox);

        connect(itemBox , &ListItemBox::setIsLike , this , [=](bool isLike){
               emit updateLikeMusic(isLike , it->getMusicID());
        });
    }

    //立马重绘
    //update();
    repaint();
}

void CommanPage::addMusicToPlayer(MusicList &musicList, QMediaPlaylist *playList)
{
    for(auto music : musicList)
    {
        switch(_pageType)
        {
        case PAGE_LOCAL:
            playList->addMedia(music.getMusicUrl());
            break;
        case PAGE_HISTORY:
            if(music.getIsHistory())
                playList->addMedia(music.getMusicUrl());
            break;
        case PAGE_LIKE:
            if(music.getIsLike())
                playList->addMedia(music.getMusicUrl());
            break;
        }
    }
}

QString CommanPage::getMusicIdByIndex(int index)
{
    if(index >= musicOfPage.size())
    {
        qDebug()<<"⽆此歌曲";
        return "";
    }
    return musicOfPage[index];
}


